import 'dart:io';

import 'package:flutter/services.dart';
import 'package:giwiki/src/rust/api/local.dart';
import 'package:giwiki/src/rust/api/api.dart' as api;

const mobile = Mobile._();

class Mobile {
  const Mobile._();

  static const _mobile = MethodChannel("mobile");

  Future<String> mobileRoot() async {
    return await _mobile.invokeMethod("mobileRoot");
  }

  Future<bool> getKeepScreenOn() async {
    return await _mobile.invokeMethod("getKeepScreenOn");
  }

  Future setKeepScreenOn(bool keepScreenOn) async {
    return await _mobile.invokeMethod("setKeepScreenOn", keepScreenOn);
  }
}

var _init = false;

Future initRoot() async {
  if (_init) {
    return;
  }
  _init = true;
  String documentRoot;
  if (Platform.isIOS || Platform.isAndroid) {
    documentRoot = await mobile.mobileRoot();
  } else if (Platform.isMacOS || Platform.isLinux || Platform.isWindows) {
    documentRoot = await desktopRoot();
  } else {
    throw "没有适配的平台";
  }
  print("documentRoot: $documentRoot");
  return api.initRoot(rootPath: documentRoot);
}
